<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */

class Category extends MX_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->library('Admin_Layout');
        $this->load->model('category/category_model');
    }
    
    public function index()
    {
        $this->mycommon->authAdmin();
        $data = array();
        $data['error'] = '';
        $this->load->library('Form_validation');
        $this->form_validation->CI =& $this;
        $this->form_validation->set_error_delimiters('<li>','</li>');
        $data['parentCategory'] = $this->category_model->getParentCategory();
        if ($this->input->post('submit') && $this->input->post('frmToken') && $this->session->userdata('frmToken') && $this->input->post('frmToken') == $this->session->userdata('frmToken')) {
            $this->form_validation->set_rules('name_vn','Tên chỉ mục(tiếng việt)', 'required|min_length[3]');
            // $this->form_validation->set_rules('name_en', 'Tên chỉ mục(tiếng anh)', 'required|min_length[3]');

            if ($this->input->post('check_href') == '1') {
                $this->form_validation->set_rules('hrefUrl', 'Đường dẫn bên thứ ba', 'required|callback_checkUrl');
            }
            $this->form_validation->set_message('required','%s không được để trống');
            $this->form_validation->set_message('min_length','%s ít nhất %d ký tự');
            $this->form_validation->set_message('checkUrl', '%s không chính xác');
            if ($this->form_validation->run() == TRUE) {
                $parent_id = $this->input->post('parentid');
                $name_vn = $this->input->post('name_vn');
                // $name_en = $this->input->post('name_en');
                $name_en = '';
                $href = $this->input->post('hrefUrl');
				$type = $this->input->post('type');
				
				//type : 1- sản phẩm , : 2 - tin tức
                if (! $this->category_model->categoryExists($name_vn)) {
                    if ($this->category_model->addCategory($name_vn, $name_en, $parent_id, $href, $type)) {
                        redirect('admin/category/organize');
                    }
                }
            }
        }
        $data['frmToken'] = uniqid();
        $this->session->set_userdata('frmToken', $data['frmToken']);
        $this->admin_layout->view('category/add_category_view', $data);
    }
    
    public function checkUrl($url)
    {
        if ($this->mycommon->isUrl($url)) {
            return true;
        }
        return false;
    }
    
    public function organize()
    {
        $this->mycommon->authAdmin();
        $data = array();
        $tmp = $this->category_model->getParentCategory(false);
        $data['parentCategory'] = $tmp;
        foreach($tmp as $k => $v) {
            $data['parentCategory'][$k]['childrenCategory'] = $this->category_model->getChildrenCategory($v['id'], false);
        }
        $this->admin_layout->view('category/organize_view', $data);
    }
    
    public function hide($catId = '')
    {
        $this->mycommon->authAdmin();
        if ($catId == '') {
            show_404();
        }
        $data = array(
            'status' => '0'
        );
        $this->category_model->updateCategoryStatus($catId, $data);
        redirect('admin/category/organize');
    }
    
    public function show($catId = '')
    {
        $this->mycommon->authAdmin();
        if ($catId == '' && intval($catId) != 0) {
            show_404();
        }
        $catId = intval($catId);
        $data = array(
            'status' => '1'
        );
        $this->category_model->updateCategoryStatus($catId, $data);
        redirect('admin/category/organize');
    }
    
    public function delete($catId = '')
    {
        $this->mycommon->authAdmin();
        if ($catId == '' && intval($catId) != 0) {
            show_404();
        }
        $catId = intval($catId);
        $this->load->model('setting/setting_model');
        $this->category_model->deleteCategory($catId);
        $lstMenubar = $this->setting_model->getOptionByName('lstMenubar');
        $lstBoxHome = $this->setting_model->getOptionByName('lstBoxHome');
        $arrMenubar = ($lstMenubar) ? explode(',', $lstMenubar['value']) : '';
        $arrBoxHome = ($lstBoxHome) ? explode(',', $lstBoxHome['value']) : '';
        foreach($arrMenubar as $k=>$v) {
            if ($v == $catId) {
                unset($arrMenubar[$k]);
                $arrMenubar = array_values($arrMenubar);
                break;
            }
        }
        foreach($arrBoxHome as $_k=>$_v) {
            if ($_v == $catId) {
                unset($arrBoxHome[$_k]);
                $arrBoxHome = array_values($arrBoxHome);
                break;
            }
        }
        $lstMenubar = implode(',', $arrMenubar);
        $lstBoxHome = implode(',', $arrBoxHome);
        $this->setting_model->setOption('lstMenubar', $lstMenubar);
        $this->setting_model->setOption('lstBoxHome', $lstBoxHome);
        redirect('admin/category/organize');
    }
    
    public function edit($catId = '')
    {
        $this->mycommon->authAdmin();
        if ($catId == '' && intval($catId) != 0) {
            show_404();
        }
        $catId = intval($catId);
        $data = array();
        $data['error'] = '';
        $this->load->library('Form_validation');
        $this->form_validation->CI =& $this;
        $this->form_validation->set_error_delimiters('<li>','</li>');
        if ($this->input->post('frmToken') && $this->session->userdata('frmToken') && $this->input->post('frmToken') == $this->session->userdata('frmToken')) {
            $this->form_validation->set_rules('name_vn','Tên chỉ mục(tiếng việt)', 'required|min_length[3]');
            //$this->form_validation->set_rules('name_en', 'Tên chỉ mục(tiếng anh)', 'required|min_length[3]');

            if ($this->input->post('check_href') == '1') {
                $this->form_validation->set_rules('hrefUrl', 'Đường dẫn bên thứ ba', 'required|callback_checkUrl');
            }
            $this->form_validation->set_message('required','%s không được để trống');
            $this->form_validation->set_message('min_length','%s ít nhất %d ký tự');
            $this->form_validation->set_message('checkUrl', '%s không chính xác');
            if ($this->form_validation->run() == TRUE) {
                $parent_id = $this->input->post('parentid');
                $name_vn = $this->input->post('name_vn');
				$type = $this->input->post('type');
                //$name_en = $this->input->post('name_en');
                $name_en = '';
                $href = $this->input->post('hrefUrl');
                $array = array(
                    'parent_id' =>  $parent_id,
                    'name_vn'   =>  $name_vn,
                    'name_en'   =>  $name_en,
                    'href'      =>  $href,
                    'type'		=> $type,
                );
                if ($this->category_model->updateCategory($catId, $array)) {
                    redirect('admin/category/organize');
                }
            }
        }
        $tmp = $this->category_model->getCategoryInfo($catId);
        $data['categoryInfo'] = $tmp[0];
        $data['frmToken'] = uniqid();
        $data['parentCategory'] = $this->category_model->getParentCategory();
        $this->session->set_userdata('frmToken', $data['frmToken']);
        $this->admin_layout->view('category/edit_category_view', $data);
    }

}
?>
